package com.springboot.datasource;

import com.alibaba.druid.spring.boot.autoconfigure.DruidDataSourceBuilder;
import org.springframework.beans.factory.annotation.Qualifier;
import org.springframework.boot.context.properties.ConfigurationProperties;
import org.springframework.context.annotation.Bean;
import org.springframework.context.annotation.Configuration;
import org.springframework.context.annotation.Primary;
import org.springframework.jdbc.core.JdbcTemplate;

import javax.sql.DataSource;

/**
 * @author peter
 * @title: DataSourceConfig
 * @projectName springAll-peter
 * @description: TODO
 * @date 19-5-5下午7:22
 */
@Configuration
public class DataSourceConfig {


    @Primary
    @Bean(name = "test1datasource")
    @ConfigurationProperties("spring.datasource.druid.mysql.test1")
    public DataSource dataSourceOne() {
        return DruidDataSourceBuilder.create().build();
    }


    @Bean(name = "test2datasource")
    @ConfigurationProperties("spring.datasource.druid.mysql.test2")
    public DataSource dataSourceTwo() {
        return DruidDataSourceBuilder.create().build();
    }


    @Bean(name = "test1JdbcTemplate")
    public JdbcTemplate primaryJdbcTemplate(@Qualifier("test1datasource") DataSource dataSource) {
        return new JdbcTemplate(dataSource);
    }

    @Bean(name = "test2JdbcTemplate")
    public JdbcTemplate secondaryJdbcTemplate(@Qualifier("test2datasource") DataSource dataSource) {
        return new JdbcTemplate(dataSource);
    }


}
